<?php
    class AdminParametersComponent {

        function parseConditions($params){
            $conditions = array();
            foreach($params as $key => $value){
                if($value != ""){
                    if(is_numeric(substr($value, 0, 4)) && is_numeric(substr($value, 5, 2)) && is_numeric(substr($value, 8, 2))){
                        $conditions[$key." >= "] = $value." 00:00:00";
                        $conditions[$key." <= "] = $value." 23:59:59";
                    }else if(is_numeric(substr($value, 0, 4))){
                        $conditions[$key] = $value;
                    }else{
                        $conditions[$key." LIKE"] = "%".$value."%";
                        
                    }
                }
            }
            
            return $conditions;
        }

	function getFindParameters($conditions, $count, $page, $limit, $model, $orderColumn, $ascDesc, $fields, $recursive){

		if($count/$limit < $page - 1){
		    $page = floor($count / $limit);
		}

		$find = array(
		         "conditions"   => $conditions,
                         "fields"       => $fields,
		         "page"         => (int)$page,
		         "limit"        => (int)$limit,
		         "order"        => $orderColumn." ".$ascDesc,
		         "recursive"    => $recursive
		);

		return $find;
	}

	function getPageInfo($count, $findParams){
		$pageInfo = array(
		    "total" => (int)$count,
		    "page"  => (int)$findParams["page"],
		    "limit" => (int)$findParams["limit"]
		);

		return $pageInfo;
	}

        function parseParams($params){
            $parsedParams = array();
            foreach($params as $key => $value){
                $parsedParams[str_replace("->", ".", $key)] = $value;
            }

            return $parsedParams;
        }

	function getListData($model, $fields, $params, $page, $limit, $orderColumn, $ascDesc, $recursive='0'){

        $params = $this->parseParams($params);

        $orderColumn = str_replace("->", ".", $orderColumn);

		$conditions = $this->parseConditions($params);

		$count = $model->find("count", array("conditions" => $conditions, "recursive" => 0));

		$findParams = $this->getFindParameters($conditions, $count, $page, $limit, $model, $orderColumn, $ascDesc, $fields, $recursive);

		/**
		 * @since 2010-04-28
		 * Switched from using ['page'] to ['offset'] b/c ['page'] wasn't generating the right LIMIT
		 */
		$offset = $page * $limit;
		$pageBak = $page;
		$page = 0;
		$findParams['offset'] = $offset;
		$findParams['page'] = $page;

		$data = $model->find("all", $findParams);

		$findParams['page'] = $pageBak + 1;

		$pageInfo = $this->getPageInfo($count, $findParams);

		return array("data"=>$data, "pageInfo" => $pageInfo);
	}

	function doSave($model, $params){
		$arrSave = $params;

		$op = $params["op"];

		if(@$arrSave["password"] != @$arrSave["password_test"] || @$arrSave["password"] == ""){
		    unset($arrSave["password"]);
		}else{
                    $arrSave["password"] = md5($arrSave["password"]);
                }

		unset($arrSave["password_test"]);
		unset($arrSave["op"]);

		if($op == "del"){
		    $model->id = $arrSave["id"];
		    if($model->delete()){
		        return true;
		    }else{
		        return false;
		    }
		}else if($op == "edit"){
		    $model->read(null, $arrSave["id"]);
		    $model->set($arrSave);
		    if($model->save()){
		        return true;
		    }else{
		        return false;
		    }
		}else if($op == "add"){
		    unset($arrSave["id"]);
		    $model->create($arrSave);
		    if($model->save()){
		        return true;
		    }else{
		        return false;
		    }
		}
	}
    }
?>
